package com.jinlia.show.modules.system.controller.sys;

import com.jinlia.show.modules.base.model.po.SysDeptPO;
import com.jinlia.show.modules.system.model.Option;
import com.jinlia.show.modules.base.model.form.DeptForm;
import com.jinlia.show.modules.base.model.query.DeptQuery;
import com.jinlia.show.modules.system.service.SysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("dept")
public class SysDeptController {
    private final SysDeptService deptService;

    @PostMapping("get")
    public ResponseEntity<List<SysDeptPO>> listDepartments(
            @RequestBody DeptQuery queryParams
    ) {
        List<SysDeptPO> sysDeptVOS = deptService.listDepartments(queryParams);
        return ResponseEntity.ok(sysDeptVOS);
    }

    @GetMapping("/options")
    public ResponseEntity<List<Option>> listDeptOptions() {
        List<Option> list = deptService.listDeptOptions();
        return ResponseEntity.ok(list);
    }

    //(summary = "获取部门表单数据")
    @GetMapping("/vo/{deptId}")
    public ResponseEntity<SysDeptPO> getDeptForm(
            @PathVariable Long deptId
    ) {
        SysDeptPO deptForm = deptService.getDeptForm(deptId);
        return ResponseEntity.ok(deptForm);
    }

    @PostMapping("add")
    public ResponseEntity saveDept(@RequestBody DeptForm formData
    ) {
        Long id = deptService.saveDept(formData);
        return ResponseEntity.ok(id);
    }

    @PutMapping(value = "/update/{deptId}")
    public ResponseEntity updateDept(
            @PathVariable Long deptId,
            @RequestBody SysDeptPO formData
    ) {
        deptId = deptService.updateDept(deptId, formData);
        return ResponseEntity.ok(deptId);
    }

    @DeleteMapping("/del/{ids}")
    public ResponseEntity deleteDepartments(@PathVariable("ids") String ids
    ) {
        boolean result = deptService.deleteByIds(ids);
        return ResponseEntity.ok(result);
    }
}
